package com.seawaterbt.springbootshiro.config;

import net.bull.javamelody.MonitoringFilter;
import net.bull.javamelody.MonitoringSpringAdvisor;
import net.bull.javamelody.SessionListener;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JavaMelodyConfiguration {

    @Bean
    public FilterRegistrationBean monitorFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new MonitoringFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean sessionListener() {
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
        servletListenerRegistrationBean.setListener(new SessionListener());
        return servletListenerRegistrationBean;
    }

    @Bean
    public MonitoringSpringAdvisor monitoringAdvisor(@Qualifier("pointcut") Pointcut pointcut){
        MonitoringSpringAdvisor monitoringSpringAdvisor = new MonitoringSpringAdvisor();
        monitoringSpringAdvisor.setPointcut(pointcut);
        return monitoringSpringAdvisor;
    }

    @Bean
    public JdkRegexpMethodPointcut pointcut(){
        JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
        pointcut.setPattern("com.seawaterbt.springbootshiro.service.*.*");
        return pointcut;
    }
}
